#include <sysbios/nano/Kernel.h>
#include <xdc/runtime/System.h>

#include "package/internal/TimerB.xdc.h"
#include  "msp430x22x4.h"

/*
 *  ======== getTime ========
 */
UInt16 TimerB_getTime()
{
    return TBR;
}

/*
 *  ======== start ========
 */
Void TimerB_start( UInt16 dt, Kernel_Event* expired )
{
    module->expired = expired;

    TBCCTL0 = CCIE;             /* TBCCR0 interrupt enabled */
    TBCCR0 = dt * 12;           /* 8MHz */
    TBCTL = TBSSEL_1 + MC_1;    /* ACLK, upmode */
}

/*
 *  ======== stop ========
 */
Void TimerB_stop()
{
    TBCTL = MC_0;
}

/* -------- internal functions -------- */

/*
 *  ======== isr ========
 */
Void TimerB_isr()
{
    TimerB_stop();

    if (module->expired != NULL) {
        Kernel_post(module->expired);
    }
}
